package edu.mse.camel.client.serverlib.notused;

import javax.naming.InitialContext;
import java.util.Hashtable;
import java.util.HashMap;
import java.util.Properties;

import javax.naming.NamingException;
import camel.client.CamelRemote;


public class ServiceLocator {
	
	
	private static ServiceLocator me;
	private InitialContext context;
	private static HashMap<String,Object> ejbMap;
	private CamelRemote remote;
	

	private ServiceLocator() throws Exception{
	   context = getInitialContext();
       ejbMap = new HashMap<String,Object>();
	}

	public static ServiceLocator getInstance() throws Exception {
		if(me == null){
			me = new ServiceLocator();
			return me;
		}
		return me;
	}
	private InitialContext getInitialContext() {
		try {
			Hashtable<String,String> properties = new Hashtable<String,String>(2);
            properties.put("java.naming.provider.url", "jnp://128.2.188.131:1099");
            properties.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");  
	        return new InitialContext(properties);
		}
		catch(NamingException ne) {
			ne.printStackTrace();
		}
		return null;
	}
	
	public CamelRemote getRemoteInterface(String name) {
		try{
			Object tempObj = ejbMap.get(name);
			if (tempObj != null) {
				CamelRemote remote = (CamelRemote)tempObj;
				return remote;
			} else {
				CamelRemote remote = (CamelRemote)context.lookup(name);
				ejbMap.put(name,remote);
				return  remote;
			}
		}catch(NamingException ne){
			ne.printStackTrace();
        }
		return null;
	}
	
	
}